<template>
  <div class="example-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <h2>{{ $t('welcome') }}</h2>
          <el-button @click="toggleLanguage" type="primary" icon="Language">
            {{ currentLocale === 'zh-CN' ? 'English' : '中文' }}
          </el-button>
        </div>
      </template>
      
      <p>{{ $t('message') }}</p>
      
      <div class="button-group">
        <el-button type="primary">{{ $t('button.submit') }}</el-button>
        <el-button type="default">{{ $t('button.cancel') }}</el-button>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { useI18n } from 'vue-i18n'
import { ref, watch } from 'vue'

const { locale } = useI18n()
const currentLocale = ref(locale.value)

// 切换语言
const toggleLanguage = () => {
  const newLocale = currentLocale.value === 'zh-CN' ? 'en' : 'zh-CN'
  currentLocale.value = newLocale
  locale.value = newLocale
  localStorage.setItem('language', newLocale)
}

// 监听语言变化
watch(locale, (newVal) => {
  currentLocale.value = newVal
})
</script>

<style scoped>
.example-container {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.button-group {
  margin-top: 20px;
  display: flex;
  gap: 10px;
}
</style>
